package com.example.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.First;
import com.example.entity.Income;
import com.example.entity.Outcome;
import com.example.mapper.IncomeMapper;
import com.example.mapper.OutcomeMapper;
import com.example.service.IFirstService;
import com.example.service.IIncomeService;
import com.example.service.IOutcomeService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class ExcelTask {

    private final IFirstService iFirstService;
    private final IIncomeService iIncomeService;
    private final IncomeMapper incomeMapper;
    private final IOutcomeService iOutcomeService;
    private final OutcomeMapper outcomeMapper;
//    /**
//     * 开启定时任务
//     */
//    @Scheduled(cron = "0/5 * * * * ? ")
//    public void executeTask(){
//        log.info("定时任务开启：{}",new Date());
//    }


    public void excelCreate(){

    }
//    @Scheduled(cron = "0 0 0 1 * ? *")//每月一号执行生成一份月报
//    public void exportBudgetData(HttpServletResponse response) throws URISyntaxException, IOException {
//        //获取这个月的开始与结尾
//        LocalDate end = LocalDate.now();
//        LocalDate begin = end.plusMonths(-1);
//
//        //查询数据
//        List<First> firsts = iFirstService.list(new LambdaQueryWrapper<First>()
//                .eq(First::getStatus,0));
//
//        //获取准备好的Excel文件格式
//        InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/收支月报.xlsx");
//        try {
//            // 编辑时间
//            XSSFWorkbook excel = new XSSFWorkbook(in);
//            XSSFSheet sheet = excel.getSheet("Sheet1");
//            sheet.getRow(1).getCell(1).setCellValue("时间：" + begin + "至" + end);
//            for (int i = 0; i < firsts.size(); i = i + 3) {
//                XSSFRow row = sheet.getRow(3 + i / 3);
//                row.getCell(2).setCellValue(firsts.get(i).getTypeName());
//                row.getCell(3).setCellValue(!firsts.get(i).getJudge() ? incomeMapper.getAmounts(begin,end,firsts.get(i).getId()) : outcomeMapper.getAmounts4(begin,end,firsts.get(i).getId()));
//                row.getCell(4).setCellValue(firsts.get(i + 1).getTypeName());
//                row.getCell(5).setCellValue(!firsts.get(i + 1).getJudge() ? incomeMapper.getAmounts(begin,end,firsts.get(i).getId()) : outcomeMapper.getAmounts4(begin,end,firsts.get(i).getId()));
//                row.getCell(6).setCellValue(firsts.get(i + 2).getTypeName());
//                row.getCell(7).setCellValue(!firsts.get(i + 2).getJudge() ? incomeMapper.getAmounts(begin,end,firsts.get(i).getId()) : outcomeMapper.getAmounts4(begin,end,firsts.get(i).getId()));
//            }
//            List<Income> incomes = iIncomeService.list(new LambdaQueryWrapper<Income>()
//                    .between(Income::getTime, begin, end)
//                    .groupBy(Income::getTime));
//            List<Outcome> outcomes = iOutcomeService.list(new LambdaQueryWrapper<Outcome>()
//                    .between(Outcome::getTime, begin, end)
//                    .groupBy(Outcome::getTime));
//            for (int i = 0; i < incomes.size(); i++) {
//                XSSFRow row = sheet.getRow(3 + i);
//                row.getCell(8).setCellValue(incomes.get(i).getTime().toString());
//                row.getCell(9).setCellValue(iFirstService.getById(incomes.get(i).getTypeKey()).getTypeName());
//                row.getCell(10).setCellValue(incomes.get(i).getAmount());
//            }
//            for (int i = 0; i < outcomes.size(); i++) {
//                XSSFRow row = sheet.getRow(3 + i);
//                row.getCell(8).setCellValue(outcomes.get(i).getTime().toString());
//                row.getCell(9).setCellValue(iFirstService.getById(outcomes.get(i).getTypeKey1()).getTypeName());
//                row.getCell(10).setCellValue(outcomes.get(i).getAmount());
//            }
//
//            //使用HttpClient发送请求到Flask服务器
//            CloseableHttpClient httpClient = HttpClients.createDefault();
//
//            URI uri = new URIBuilder()
//                    .setScheme("http")
//                    .setHost("localhost:5000")
//                    .setPath("predict")
//                    .build();
//            HttpGet httpGet = new HttpGet(uri);
//            //发送请求
//            CloseableHttpResponse rep = httpClient.execute(httpGet);
//
//            HttpEntity entity = rep.getEntity();
//            String res = EntityUtils.toString(entity);
//            //在对应单元格添加分析结果
//            sheet.getRow(0).getCell(8).setCellValue(res);
//            // 通过输出流将Excel文件下载到客户端浏览器
//            ServletOutputStream out = response.getOutputStream();
//            excel.write(out);
//
//            // 关闭资源
//            out.close();
//            excel.close();
//
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//    }
}
